XPath - Part 6: Filtering with Sub-Nodes

Objective

To filter at multiple node levels in an XML document while creating a document which displays the returned data.

Background Information

XML Files can have nodes, children nodes, grand-children nodes, and an unlimited amount of levels after that. Not only that, but values and attributes can be stored at each node level including the root level, so it may be necessary to filter at several node levels. Unfortunately we don’t have any XML files this complex lying around, so we will be demonstrating this concept in a rather simple manner, but combining this tutorial with the previous XPath Wizard tutorials, you should have all the knowledge you need to make any level of complex multi-level filters in XML documents, the only limiting factor will be how much scratch paper you have to draw all the trees of data!

Pre-requisites

Inserting a Tag, ForEach Tag, XPath - Part 1: Basic Selection, XPath - Part 2: Sorting with Orderby, XPath - Part 4: Filtering with Variable Values

Starting Template

This tutorial builds on fundamentals learned in the other XPath Wizard tutorials, so we’ll be starting with the template made in XPath - Part 4: Filtering with Variable Values. If you don’t have that template, it is shown below and step-by-step instructions on creating are available in the previous XPath Tutorials.

Procedure

1. Open XPath Wizard

First, open the XPath Wizard by selecting the ForEach tag and clicking Wizard under the AutoTag ribbon.

Show Me How!

2. Add node OrderDetails/OrderDetail

Now we’ll add a sub-node. We have already applied a filter to the top level, but by adding a sub-node we can now apply additional conditions to this level. We won’t be applying any conditions here, but now you can see how it can be done. Close the XPath Wizard when you are done with this.

Show Me How!

3. Change the first column title to Product and third to Quantity

Now, we have a different set of data selected. Instead of Orders, we have OrderDetails selected, so we need to change what data is displayed. Change the first column name to Product and the third to Quantity.

Show Me How!

4. Change the first and third tags to ProductID and Quantity

As part of changing what data is displayed, we need to change the out tags as well. Here we’ll change the 1st out tag to ProductID and the 3rd to Quantity (using the Data tree as always). The 2nd tag needs to be changed too, but we no longer have a Shipper ID to display

Show Me How!

5. Change the Shipper tag to the $(ShipperID) variable

You might remember before we filtered the ShipVia node so that only one ShipperID is shown. Well this is beneficial to us now because instead of displaying the ShipVia node, we can simply display the $(ShipperID) variable we created before. Change the 2nd out tag’s data to the ShipperID via the Data tree.

Show Me How!

6. Generate

Finally, generate the document and view the results! There is a lot of power in the feature demonstrated here, but you might not always find that it is necessary. Keep filters simple in the beginning. If you need a complex filter like shown here, you will know.

Show Me How!

Quiz Yourself!

How many levels of sub-nodes can you filter?
Unlimited! This makes for a very powerful Wizard when necessary, but not everyone needs this much filtering power!
I experimented with it myself and it only works up to 12.
As the tutorial showed, I can only use 2 levels and they must be adjacent.
As was iterated throughout this tutorial, I shouldn’t filter sub-nodes, only one level
When is multi-level filtering like this necessary?
Truthfully, not very often. XML standards provide the capability to create complex structures in XML documents, so we provide a way to filter them
Never ever. It should never be used. It’s bad voodoo because it causes big headaches
It isn’t, but it makes things simpler. All filtering can be done in a single node level.
All the time. If I can’t figure out how to apply multi-level filtering to my data, I need to upgrade my data

Congratulations!

You have completed this tutorial. We recommend taking a look at the OData - Part 1: Basic Selection tutorial next!

close
continue